package com.loveyukari.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.loveyukari.common.exception.InternalException;
import com.loveyukari.model.Auth;
import com.loveyukari.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loveyukari.model.LiwuStyle;
import com.loveyukari.support.JsonResult;

import net.sf.json.JSONObject;

/**
*
*yangshaohua
*2017年7月26日下午4:32:15
**/
@Controller
@RequestMapping("/admin/draw")
public class DrawController {
	
	@Autowired
	private DrawChatmsgService drawChatmsgService;
	
	@Autowired
	private DrawLiwuService drawLiwuService;
	
	@Autowired
	private DrawTicketService drawTicketService;

	@Autowired
	private TmpService tmpService;

	@RequestMapping("/chatmsg/index")
	public String drawChatmsgIndex(Model model){
		JSONObject status = drawChatmsgService.getStatus();
		model.addAttribute("status", status);
		return "draw/chatmsg_index";
	}
	
	@RequestMapping("/chatmsg/authDetail")
	@ResponseBody
	public JsonResult<Object> drawChatmsgauthDetail(){
		List<JSONObject> list = drawChatmsgService.getAuthDetail();
		return new JsonResult<Object>(0,"",list);
	}
	@RequestMapping("/chatmsg/start")
	@ResponseBody
	public JsonResult<Object> drawChatmsgStart(String chatmsg,int count,int minBl){
		String startTime = drawChatmsgService.start(chatmsg,count,minBl);
		return new JsonResult<Object>(0,"",startTime);
	}
	
	@RequestMapping("/chatmsg/stop")
	@ResponseBody
	public JsonResult<Object> drawChatmsgStop(){
		JSONObject result = drawChatmsgService.stop();
		return new JsonResult<Object>(0,"",result);
	}
	
	@RequestMapping("/chatmsg/clear")
	@ResponseBody
	public JsonResult<Object> drawChatmsgClear(){
		drawChatmsgService.clear();
		return new JsonResult<Object>();
	}
	
	@RequestMapping("/liwu/index")
	public String drawLiwuIndex(Model model){
		JSONObject status = drawLiwuService.getStatus();
		model.addAttribute("status", status);
		return "draw/liwu_index";
	}
	
	@RequestMapping("/liwu/start")
	@ResponseBody
	public JsonResult<Object> drawLiwuStart(int gfid,int count){
		String startTime = drawLiwuService.start(gfid,count);
		return new JsonResult<Object>(0,"",startTime);
	}
	
	@RequestMapping("/liwu/stop")
	@ResponseBody
	public JsonResult<Object> drawLiwuStop(){
		JSONObject result = drawLiwuService.stop();
		return new JsonResult<Object>(0,"",result);
	}
	
	@RequestMapping("/liwu/clear")
	@ResponseBody
	public JsonResult<Object> drawLiwuClear(){
		drawLiwuService.clear();
		return new JsonResult<Object>();
	}
	
	@RequestMapping("/liwu/reloadStyle")
	@ResponseBody
	public JsonResult<Object> drawLiwuReloadStyle(){
		List<LiwuStyle> list = drawLiwuService.loadLiwuStyle();
		return new JsonResult<Object>(0,"",list);
	}
	
	@RequestMapping("/ticket/index")
	public String drawTicketIndex(Model model){
		JSONObject status = drawTicketService.getStatus();
		model.addAttribute("status", status);
		return "draw/ticket_index";
	}
	
	@RequestMapping("/ticket/getResult")
	@ResponseBody
	public JsonResult<Object> drawTicketGetResult(String[] key,String problem){
		JSONObject result = drawTicketService.getResult();
		return new JsonResult<Object>(0,"",result);
	}
	@RequestMapping("/ticket/start")
	@ResponseBody
	public JsonResult<Object> drawTicketStart(String[] key,String problem){
		String message = drawTicketService.start(key, problem);
		return new JsonResult<Object>(0,"",message);
	}
	
	@RequestMapping("/ticket/clear")
	@ResponseBody
	public JsonResult<Object> drawTicketClear(){
		drawTicketService.clear();
		return new JsonResult<Object>();
	}
	
	@RequestMapping("/ticket/show")
	public String drawTicketShow(){
		return "draw/ticket_show";
	}


	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@RequestMapping("/christmas/show")
	public String christmasShow(@RequestParam(defaultValue = "1")int size,@RequestParam(defaultValue = "")String group,Model model) {
		List<Auth> list = tmpService.getCarnivalAuth();
		model.addAttribute("size",size);
		model.addAttribute("list",list);
		model.addAttribute("group",group);
		return "draw/christmas_show";
	}

	private int authId = 0;


	@RequestMapping("/christmas/draw")
	@ResponseBody
	public JsonResult christmasDraw(int size,String group)  {
		try {
			List<Auth> list = tmpService.drewCarnivalAuth(size, group, this.authId);
			return new JsonResult(0,"",list);
		} catch (InternalException e) {
			return new JsonResult(1,e.getMessage());
		}
	}




	@RequestMapping("/christmas/setFlag")
	@ResponseBody
	public JsonResult christmasSetFlag(@RequestParam(defaultValue = "false")boolean flag,@RequestParam(defaultValue = "true")boolean initFlag,@RequestParam(defaultValue="0")int time,String defaultName,@RequestParam(defaultValue="-1")int authId){
		if (authId != -1){
			this.authId = authId;
		}
		return new JsonResult(0,"");
	}

	@RequestMapping("/christmas/manage")
	public String christmasManage(Model model){
		model.addAttribute("authId",this.authId);
		return "draw/christmas_manage";
	}

	@RequestMapping("/christmas/createUrl")
	public String christmasCreateUrl(Model model){
		model.addAttribute("size",1);
		return "draw/christmas_create_url";
	}


	
	
}
